package tom.client.invoice;

import java.util.Map;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;

import tom.shared.Contact;
import tom.shared.Customer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class FormCustomer extends DataForm {
	private static FormCustomer panel;

	public static FormCustomer getPanel() {
		if (panel == null)
			panel = GWT.create(FormCustomer.class);
		return panel;
	}

	private final TextBox txtTitle;

	private Customer record;

	public FormCustomer() {
		txtCode.setMaxLength(6);
		lbCode.setText("รหัสลูกค้า");

		txtTitle = new TextBox();
		txtTitle.setEnabled(false);
		txtTitle.setMaxLength(0);
		add(txtTitle, 438, 74);
		txtTitle.setSize("269px", "18px");
		txtTitle.setReadOnly(true);

		Label lblNewLabel = new Label("ชื่อลูกค้า");
		add(lblNewLabel, 340, 74);

		lbModUserTime = new Label("...");
		lbModUserTime.setWordWrap(false);
		add(lbModUserTime, 434, 434);
		lbModUserTime.setSize("335px", "21px");

		custLimit = new DoubleBox();
		custLimit.setAlignment(TextAlignment.RIGHT);
		add(custLimit, 122, 106);

		Label lblNewLabel_1 = new Label("วงเงิน");
		add(lblNewLabel_1, 23, 110);

		custBalance = new DoubleBox();
		custBalance.setAlignment(TextAlignment.RIGHT);
		custBalance.setEnabled(false);
		custBalance.setReadOnly(true);
		add(custBalance, 122, 138);

		Label lblNewLabel_2 = new Label("ค้างชำระ");
		add(lblNewLabel_2, 23, 142);

		Label lblNewLabel_4 = new Label("ระยะหนี้(วัน)");
		add(lblNewLabel_4, 23, 172);

		Label lblNewLabel_5 = new Label("วิธีชำระเงิน");
		add(lblNewLabel_5, 23, 230);

		Label lblNewLabel_3 = new Label("สถานะ");
		add(lblNewLabel_3, 23, 201);

		custVatCode = new TextBox();
		add(custVatCode, 122, 256);
		custVatCode.setSize("65px", "16px");

		Label lblNewLabel_6 = new Label("ภาษี");
		add(lblNewLabel_6, 23, 259);

		custLastDate = new TextBox();
		add(custLastDate, 122, 286);
		custLastDate.setSize("132px", "16px");
		custLastDate.setReadOnly(true);

		Label lblNewLabel_7 = new Label("วันที่บิลล่าสุด");
		add(lblNewLabel_7, 23, 289);

		Label label = new Label("เลขที่บิลล่าสุด");
		add(label, 23, 319);

		custLabel = new TextArea();
		add(custLabel, 438, 234);
		custLabel.setSize("282px", "109px");

		Label lblNewLabel_8 = new Label("หมายเหตุ");
		add(lblNewLabel_8, 340, 234);

		custDueDay = new TextBox();
		add(custDueDay, 122, 170);
		custDueDay.setSize("65px", "16px");

		custStatus = new TextBox();
		add(custStatus, 122, 198);
		custStatus.setSize("65px", "16px");

		Button button = new Button("?");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				FormCustomer.this.setMode(2);
				LookupDialog.show("ระยะเวลาชำระหนี้", "dueDay", custDueDay);
			}
		});
		add(button, 195, 169);

		Button button_1 = new Button("?");
		button_1.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				FormCustomer.this.setMode(2);
				LookupDialog.show("สถานะ", "stCode", custStatus);
			}
		});
		add(button_1, 195, 198);
		button_1.setSize("19px", "24px");

		Button button_2 = new Button("?");
		button_2.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				FormCustomer.this.setMode(2);
				LookupDialog.show("วิธีชำระ", "payMeth", custPayMeth);
			}
		});
		add(button_2, 195, 226);
		button_2.setSize("19px", "24px");

		Button button_3 = new Button("?");
		button_3.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				FormCustomer.this.setMode(2);
				LookupDialog.show("ภาษี", "vatCode", custVatCode);
			}
		});
		add(button_3, 195, 256);
		button_3.setSize("19px", "24px");

		custLastDocRef = new TextBox();
		add(custLastDocRef, 122, 316);
		custLastDocRef.setSize("131px", "16px");
		custLastDocRef.setReadOnly(true);

		custPayMeth = new TextBox();
		add(custPayMeth, 122, 226);
		custPayMeth.setSize("65px", "16px");
		
		txtAvailable = new TextBox();
		txtAvailable.setAlignment(TextAlignment.RIGHT);
		txtAvailable.setReadOnly(true);
		add(txtAvailable, 438, 105);
		txtAvailable.setSize("88px", "16px");
		
		Label lblNewLabel_9 = new Label("ยอดคงเหลือ");
		add(lblNewLabel_9, 340, 110);
		this.setFieldsHandler();
	}

	@Override
	protected void search() {
		LookupDialog.show("ที่อยู่/ที่ติดต่อ", "contCode", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getSaleService().selectContact(txtCode.getText(), contCallback);
	}

	@Override
	protected void update() {
		getFields();
		Library.getSaleService().updateCustomer(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		// Library.getSaleService().insertDebter(record, delCallback);
	}

	@Override
	protected void delete() {
		if (Window.confirm("ต้องการลบ Customer " + txtTitle.getText()
				+ " หรือไม่")) {
			Library.getSaleService().deleteDebter(txtCode.getText(),
					delCallback);
		}

	}

	@Override
	protected void clearFields() {
		// txtCode.setText(null);
		super.clearFields();
		txtTitle.setText(null);
		custBalance.setValue(null);
		custLimit.setValue(null);
		custDueDay.setText(null);
		custStatus.setText(null);
		custPayMeth.setText(null);
		custVatCode.setText(null);
		custLabel.setText(null);
		custLastDate.setText(null);
		custLastDocRef.setText(null);
		txtAvailable.setText(null);
		lbModUserTime.setText(null);
	}

	@Override
	protected void setFields() {
		super.setFields();
		if (record != null) {
			txtCode.setText(record.contCode);
			custBalance.setValue(record.custBalance);
			custLimit.setValue(record.custLimit);
			txtAvailable.setText(Library.moneyFormat(record.custLimit - record.custBalance));
			custDueDay.setText(record.custDueDay.toString());
			custStatus.setText(record.custStatus);
			custPayMeth.setText(record.custPayMeth.toString());
			custVatCode.setText(record.custVatCode);
			custLabel.setText(record.custLabel);
			custLastDate.setText(record.custLastDate.toString());
			custLastDocRef.setText(record.custLastDocRef);
			// dbVat.setValue(record.vat);
			// intDuePeriod.setValue(record.custDueDay);
			lbModUserTime.setText("แก้ไขล่าสุด " + record.modUser + " "
					+ Library.DATETIME_FORMAT.format(record.modTime));

		} else {
			String code = txtCode.getText();
			String title = txtTitle.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
			txtTitle.setText(title);
		}
	}

	@Override
	protected void getFields() {
		if (record == null)
			record = new Customer();
		record.contCode = txtCode.getText();
		record.custLimit = custLimit.getValue();
		record.custBalance = custBalance.getValue();
		record.custDueDay = Integer.valueOf(custDueDay.getText());
		record.custStatus = custStatus.getText();
		record.custPayMeth = Integer.valueOf(custPayMeth.getText());
		record.custVatCode = custVatCode.getText();
		record.custLabel = custLabel.getText();
		// record.vat = dbVat.getValue();
		// record.custDueDay = intDuePeriod.getValue();
	}

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	AsyncCallback<Customer> dataCallback = new AsyncCallback<Customer>() {

		@Override
		public void onSuccess(Customer result) {
			record = result;
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	private final Label lbModUserTime;

	private final DoubleBox custLimit;

	private final DoubleBox custBalance;

	private final TextBox custDueDay;

	private final TextBox custStatus;

	private final TextBox custVatCode;

	private final TextBox custPayMeth;

	private final TextBox custLastDate;

	private final TextBox custLastDocRef;

	private final TextArea custLabel;
	
	private final TextBox txtAvailable;

//	@Override
//	protected void setFieldsHandler() {
//		custLimit.addChangeHandler(changeHandler);
//		custBalance.addChangeHandler(changeHandler);
//		custDueDay.addChangeHandler(changeHandler);
//		custStatus.addChangeHandler(changeHandler);
//		custVatCode.addChangeHandler(changeHandler);
//		custPayMeth.addChangeHandler(changeHandler);
//		custLastDate.addChangeHandler(changeHandler);
//		custLastDocRef.addChangeHandler(changeHandler);
//	}

	AsyncCallback<Contact> contCallback = new AsyncCallback<Contact>() {

		@Override
		public void onSuccess(Contact result) {
			if (result != null) {
				txtTitle.setText(Library.concat(result.name, result.organiz));
				Library.getSaleService().selectCustomer(txtCode.getText(),
						dataCallback);
			} else {
				clearFields();
			}
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};
}
